
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>05-线程join方法 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../" />
    
    
    <link rel="prev" href="04-traditional-thread-state.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../">
            
                <a href="../">
            
                    
                    编辑器
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../editortools/SublineText3-Quick-Key.html">
            
                <a href="../editortools/SublineText3-Quick-Key.html">
            
                    
                    SublineText3快捷键
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../editortools/SublineText3.html">
            
                <a href="../editortools/SublineText3.html">
            
                    
                    SublineText3问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../editortools/iTerm2-guide.html">
            
                <a href="../editortools/iTerm2-guide.html">
            
                    
                    iTerm2指南
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../">
            
                <a href="../">
            
                    
                    乱七八糟
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../sundry/git-note.html">
            
                <a href="../sundry/git-note.html">
            
                    
                    Git 简单笔记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../sundry/gitbook-doc.html">
            
                <a href="../sundry/gitbook-doc.html">
            
                    
                    gitbook 教程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../sundry/gitbook-to-github.html">
            
                <a href="../sundry/gitbook-to-github.html">
            
                    
                    gitbook 集成到Github
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../sundry/git-integrate.html">
            
                <a href="../sundry/git-integrate.html">
            
                    
                    Git提速
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../sundry/markdown-doc.html">
            
                <a href="../sundry/markdown-doc.html">
            
                    
                    markdown 语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../sundry/maven-nexus.html">
            
                <a href="../sundry/maven-nexus.html">
            
                    
                    Maven 私服搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../sundry/idea-config.html">
            
                <a href="../sundry/idea-config.html">
            
                    
                    IDEA 配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../sundry/idea-quick-key.html">
            
                <a href="../sundry/idea-quick-key.html">
            
                    
                    IDEA 快捷键
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../sundry/iterm2.html">
            
                <a href="../sundry/iterm2.html">
            
                    
                    iTerm2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" >
            
                <a target="_blank" href="http://fontawesome.dashgame.com/">
            
                    
                    Font Awesome字体
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../">
            
                <a href="../">
            
                    
                    mariadb
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../mariadb/mariadb-install.html">
            
                <a href="../mariadb/mariadb-install.html">
            
                    
                    mariadb 安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../mariadb/mysql-install.html">
            
                <a href="../mariadb/mysql-install.html">
            
                    
                    mysql 安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../mariadb/mysql-base.html">
            
                <a href="../mariadb/mysql-base.html">
            
                    
                    mysql 基础知识
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../">
            
                <a href="../">
            
                    
                    MongoDB
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../mongodb/mongodb-install.html">
            
                <a href="../mongodb/mongodb-install.html">
            
                    
                    MongoDB 安装
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../">
            
                <a href="../">
            
                    
                    nginx
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../nginx/nginx-install.html">
            
                <a href="../nginx/nginx-install.html">
            
                    
                    Nginx 安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../nginx/nginx-commandline.html">
            
                <a href="../nginx/nginx-commandline.html">
            
                    
                    Ngixn 命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../nginx/nginx-virtualhost.html">
            
                <a href="../nginx/nginx-virtualhost.html">
            
                    
                    Nginx 虚拟主机
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../">
            
                <a href="../">
            
                    
                    HttpClient
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../httpclient/httpclient-timeout.html">
            
                <a href="../httpclient/httpclient-timeout.html">
            
                    
                    HttpClient几个超时
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../httpclient/httpclient-pool.html">
            
                <a href="../httpclient/httpclient-pool.html">
            
                    
                    HttpClient 连接池
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../">
            
                <a href="../">
            
                    
                    Java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../java/001-tomcat-loader-cpu-high.html">
            
                <a href="../java/001-tomcat-loader-cpu-high.html">
            
                    
                    01-Tomcat应用负载过高—CPU过高
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../">
            
                <a href="../">
            
                    
                    Spring与SpringBoot
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../SpringBoot/001-SpringBoot-form-jar-to-war.html">
            
                <a href="../SpringBoot/001-SpringBoot-form-jar-to-war.html">
            
                    
                    01-SpringBoot-Jar包转war包
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../">
            
                <a href="../">
            
                    
                    RPC
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../rpc/thrift/thrift-01-intro.html">
            
                <a href="../rpc/thrift/thrift-01-intro.html">
            
                    
                    01-Thrift简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../rpc/thrift/thrift-02-install.html">
            
                <a href="../rpc/thrift/thrift-02-install.html">
            
                    
                    02-Thrift安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../rpc/thrift/thrift-03-types.html">
            
                <a href="../rpc/thrift/thrift-03-types.html">
            
                    
                    03-Thrift数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../rpc/thrift/thrift-04-generated-code.html">
            
                <a href="../rpc/thrift/thrift-04-generated-code.html">
            
                    
                    04-Thrift代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../rpc/thrift/thrift-05-architecture.html">
            
                <a href="../rpc/thrift/thrift-05-architecture.html">
            
                    
                    05-Thrift架构设计
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../">
            
                <a href="../">
            
                    
                    线程世界
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="01-traditional-thread.html">
            
                <a href="01-traditional-thread.html">
            
                    
                    01-传统线程技术
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="02-traditional-mutex.html">
            
                <a href="02-traditional-mutex.html">
            
                    
                    02-线程之间的互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="03-traditional-communication.html">
            
                <a href="03-traditional-communication.html">
            
                    
                    03-线程之间的通信
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="04-traditional-thread-state.html">
            
                <a href="04-traditional-thread-state.html">
            
                    
                    04-线程状态
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.11.5" data-path="05-traditional-thread-join.html">
            
                <a href="05-traditional-thread-join.html">
            
                    
                    05-线程join方法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../">
            
                <a href="../">
            
                    
                    Linux
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../linux/001-linux-command.html">
            
                <a href="../linux/001-linux-command.html">
            
                    
                    01-Linux常用命令总结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="../linux/002-linux-firewall.html">
            
                <a href="../linux/002-linux-firewall.html">
            
                    
                    02-Linux防火墙
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="../linux/003-linux-tmux.html">
            
                <a href="../linux/003-linux-tmux.html">
            
                    
                    03-Linux-Tmux
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../">
            
                <a href="../">
            
                    
                    数据结构与算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../datastructure/01-intro.html">
            
                <a href="../datastructure/01-intro.html">
            
                    
                    01-简介
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../">
            
                <a href="../">
            
                    
                    前端
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../front/01-yarn.html">
            
                <a href="../front/01-yarn.html">
            
                    
                    01-yarn
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../front/02-jsx.html">
            
                <a href="../front/02-jsx.html">
            
                    
                    02-jsx语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../front/03-react-component.html">
            
                <a href="../front/03-react-component.html">
            
                    
                    03-React组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.4" data-path="../front/04-ES6.html">
            
                <a href="../front/04-ES6.html">
            
                    
                    04-ES6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.5" data-path="../front/05-webpack.html">
            
                <a href="../front/05-webpack.html">
            
                    
                    05-webpack
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >05-线程join方法</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6E90;&#x7801;&#x52A0;&#x793A;&#x4F8B;&#x5206;&#x6790;&#x7EBF;&#x7A0B;&#x7684;-join-&#x65B9;&#x6CD5;">&#x6E90;&#x7801;&#x52A0;&#x793A;&#x4F8B;&#x5206;&#x6790;&#x7EBF;&#x7A0B;&#x7684; join &#x65B9;&#x6CD5;</h1>
<p>&#x70B8;&#x4E00;&#x770B;&#x5230; join() &#x65B9;&#x6CD5;&#x53EF;&#x80FD;&#x6709;&#x70B9;&#x61F5;&#xFF0C;&#x4E0D;&#x77E5;&#x9053;&#x6B64;&#x65B9;&#x6CD5;&#x662F;&#x5E72;&#x5565;&#x7684;&#x3002;&#x4E0B;&#x9762;&#x4ECE;&#x6E90;&#x7801;&#x7684;&#x89D2;&#x5EA6;&#x5206;&#x6790;&#x4E00;&#x4E0B;&#x6B64;&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x7ED3;&#x5408;&#x76F8;&#x5E94;&#x7684;&#x793A;&#x4F8B;&#x5F7B;&#x5E95;&#x641E;&#x61C2; join() &#x65B9;&#x6CD5;&#x3002;</p>
<p><strong>&#x9996;&#x5148;&#x6211;&#x4EEC;&#x5E94;&#x8BE5;&#x77E5;&#x9053; join() &#x65B9;&#x6CD5;&#x662F;&#x5B9A;&#x4E49;&#x5728; Thread &#x7C7B;&#x91CC;&#x9762;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x8C03;&#x7528;&#x8005;&#x5FC5;&#x987B;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x7C7B;&#xFF0C;&#x5FC5;&#x987B;&#x5148;&#x8C03;&#x7528; start &#x65B9;&#x6CD5;&#x518D;&#x8C03;&#x7528; join &#x65B9;&#x6CD5;</strong><br>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-java">Thread t = new CustomThread();
t.start();   # &#x6CE8;&#x610F;&#x8981;&#x5148;&#x8C03;&#x7528; start &#x518D;&#x8C03;&#x7528; join
t.join();
</code></pre>
<p>&#x4EE3;&#x7801;&#x793A;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{
    System.out.println(<span class="hljs-string">&quot;Main started&quot;</span>);

    Thread t1 = <span class="hljs-keyword">new</span> Thread(){
        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i=<span class="hljs-number">1</span>;i&lt;=<span class="hljs-number">5</span>;i++){
                <span class="hljs-keyword">try</span> {
                    Thread.sleep(<span class="hljs-number">500</span>);
                } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                      e.printStackTrace();
                }
                System.out.println(<span class="hljs-string">&quot;t1 print &quot;</span> + i);
            }
        }
    };

    t1.start();
    t1.join();
    System.out.println(<span class="hljs-string">&quot;Main over&quot;</span>);
}

&#x8F93;&#x51FA;&#x5982;&#x4E0B;&#xFF1A;
Main started
t1 print <span class="hljs-number">1</span>
t1 print <span class="hljs-number">2</span>
t1 print <span class="hljs-number">3</span>
t1 print <span class="hljs-number">4</span>
t1 print <span class="hljs-number">5</span>
Main over
</code></pre>
<p>&#x6709;&#x8F93;&#x51FA;&#x5185;&#x5BB9;&#x53EF;&#x77E5;&#xFF0C;&#x4E3B;&#x7EBF;&#x7A0B;&#x5F00;&#x59CB;&#x8F93;&#x51FA;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x7B49;&#x5F85; t1 &#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x5B8C;&#x6210;&#xFF0C;&#x6700;&#x540E;&#x4E3B;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#x3002;&#x4F46;&#x662F; <code>t1.join()</code> &#x662F;&#x5982;&#x4F55;&#x4FDD;&#x8BC1;&#x5148;&#x6267;&#x884C; t1 &#x7EBF;&#x7A0B;&#x7684;&#x5462;&#xFF1F;&#x6211;&#x4EEC;&#x4ECE;&#x6E90;&#x7801;&#x89D2;&#x5EA6;&#x770B;&#x4E00;&#x4E0B;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x5148;&#x770B;&#x4E00;&#x4E0B;&#x6E90;&#x7801;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-comment">/**
 * Waits for this thread to die.
 *
 * &lt;p&gt; An invocation of this method behaves in exactly the same
 * way as the invocation
 *
 * &lt;blockquote&gt;
 * {<span class="hljs-doctag">@linkplain</span> #join(long) join}{<span class="hljs-doctag">@code</span> (0)}
 * &lt;/blockquote&gt;
 *
 * <span class="hljs-doctag">@throws</span>  InterruptedException
 *          if any thread has interrupted the current thread. The
 *          &lt;i&gt;interrupted status&lt;/i&gt; of the current thread is
 *          cleared when this exception is thrown.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">join</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{
    join(<span class="hljs-number">0</span>);
}
</code></pre>
<p>&#x7531;&#x6B64;&#x6E90;&#x7801;&#x53EF;&#x77E5; join &#x65B9;&#x6CD5;&#x7684;&#x4F5C;&#x7528;&#x662F;&#xFF1A;<strong>&#x7B49;&#x5F85;&#x8BE5;&#x7EBF;&#x7A0B;&#x7EC8;&#x6B62;&#x3002;</strong> &#x5E76;&#x4E14;&#x5B9E;&#x9645;&#x8C03;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#x662F; join(0)&#xFF0C;join(0) &#x7684;&#x6E90;&#x7801;&#x662F;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">synchronized</span> <span class="hljs-keyword">void</span> <span class="hljs-title">join</span><span class="hljs-params">(<span class="hljs-keyword">long</span> millis)</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{
    <span class="hljs-keyword">long</span> base = System.currentTimeMillis();
    <span class="hljs-keyword">long</span> now = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">if</span> (millis &lt; <span class="hljs-number">0</span>) {
        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(<span class="hljs-string">&quot;timeout value is negative&quot;</span>);
    }

    <span class="hljs-keyword">if</span> (millis == <span class="hljs-number">0</span>) {
        <span class="hljs-keyword">while</span> (isAlive()) {
            wait(<span class="hljs-number">0</span>);
        }
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">while</span> (isAlive()) {
            <span class="hljs-keyword">long</span> delay = millis - now;
            <span class="hljs-keyword">if</span> (delay &lt;= <span class="hljs-number">0</span>) {
                <span class="hljs-keyword">break</span>;
            }
            wait(delay);
            now = System.currentTimeMillis() - base;
        }
    }
}
</code></pre>
<p>&#x7ED3;&#x5408;&#x6E90;&#x7801;&#x548C;&#x793A;&#x4F8B;&#x5206;&#x6790;&#x53EF;&#x77E5;&#xFF0C;&#x5F53;&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x5230; <code>t1.start()</code> &#x65B9;&#x6CD5;&#x65F6;&#xFF0C;t1 &#x7EBF;&#x7A0B;&#x8FDB;&#x5165;&#x5230; <code>RUNNABLE</code> &#x72B6;&#x6001;&#xFF0C;&#x4F46;&#x662F; t1 &#x672A;&#x5FC5;&#x83B7;&#x5F97;&#x5230; CPU &#x7684;&#x8D44;&#x6E90;&#xFF0C;&#x6240;&#x4EE5; t1 &#x672A;&#x5FC5;&#x7ACB;&#x9A6C;&#x6267;&#x884C;&#x3002;<br>&#x5206;&#x4E24;&#x79CD;&#x60C5;&#x51B5;&#x5206;&#x6790;&#xFF1A;<br><strong>&#x7B2C;&#x4E00;&#xFF0C;</strong>&#x5047;&#x5982; t1 &#x83B7;&#x5F97; CPU &#x8D44;&#x6E90;&#x5E76;&#x7ACB;&#x9A6C;&#x6267;&#x884C;&#xFF0C;&#x4E14;&#x5728;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D; CPU &#x4E00;&#x76F4;&#x672A;&#x5207;&#x6362;&#xFF0C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x5F88;&#x5408;&#x4E4E;&#x60C5;&#x7406;&#xFF0C;&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x6211;&#x6545;&#x610F;&#x6DFB;&#x52A0;&#x4E86; Thread.sleep(500) &#x65B9;&#x6CD5;&#xFF0C;&#x9632;&#x6B62; CPU &#x8D44;&#x6E90;&#x4E00;&#x76F4;&#x88AB; t1 &#x5360;&#x7528;&#x7684;&#x60C5;&#x51B5;&#x3002;</p>
<p><strong>&#x7B2C;&#x4E8C;&#xFF0C;</strong>t1 &#x672A;&#x83B7;&#x5F97; CPU &#x8D44;&#x6E90;&#xFF0C;&#x800C;&#x662F;&#x7531;&#x4E3B;&#x7EBF;&#x7A0B;&#x5373; main &#x65B9;&#x6CD5;&#x6240;&#x5728;&#x7684;&#x7EBF;&#x7A0B;&#x83B7;&#x5F97; CPU &#x8D44;&#x6E90;&#xFF0C;&#x7EE7;&#x7EED;&#x6267;&#x884C; <code>t1.join()</code> &#x65B9;&#x6CD5;&#xFF0C;&#x7531; join(long millis) &#x65B9;&#x6CD5;&#x7684;&#x6E90;&#x7801;&#x53EF;&#x77E5;&#xFF0C;&#x6B64;&#x65B9;&#x6CD5;&#x662F;&#x4E00;&#x4E2A;&#x540C;&#x6B65;&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x4E14;&#x4F7F;&#x7528;&#x7684;&#x9501;&#x662F;&#x8BE5;&#x7EBF;&#x7A0B;&#x5BF9;&#x8C61;&#x672C;&#x8EAB;&#x3002;&#x6B64;&#x65F6;&#x53EA;&#x6709;&#x4E3B;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#xFF0C;&#x6CA1;&#x6709;&#x7ADE;&#x4E89;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x6240;&#x4EE5;&#x4E3B;&#x7EBF;&#x7A0B;&#x83B7;&#x5F97;&#x9501;&#xFF0C;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x5230; <code>while(isAlive())</code> &#x65F6;&#xFF0C;&#x7531;&#x4E8E; t1 &#x76EE;&#x524D;&#x8FD8;&#x5728; JVM &#x865A;&#x62DF;&#x673A;&#x4E2D;&#xFF0C;&#x7ED3;&#x679C;&#x80AF;&#x5B9A;&#x4E3A; true&#xFF0C;&#x6240;&#x4EE5;&#x4E3B;&#x7EBF;&#x7A0B;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x5230; wait(0) &#x65B9;&#x6CD5;&#xFF0C;&#x6B64;&#x65F6;&#x4E3B;&#x7EBF;&#x7A0B;&#x91CA;&#x653E;&#x9501;&#xFF0C;&#x5E76;&#x4E14;&#x5904;&#x4E8E; <code>WATING</code> &#x72B6;&#x6001;&#x3002;&#x6211;&#x4EEC;&#x90FD;&#x77E5;&#x9053;&#x5904;&#x4E8E; <code>WAITING</code> &#x72B6;&#x6001;&#x7684;&#x7EBF;&#x7A0B;&#x5982;&#x679C;&#x6709;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x8C03;&#x7528;&#x4E86; notify() &#x6216;&#x8005; notifyAll() &#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x6B64;&#x7EBF;&#x7A0B;&#x624D;&#x6709; <code>WAITING</code> &#x72B6;&#x6001;&#x8F6C;&#x53D8;&#x4E3A; <code>RUNNABLE</code> &#x72B6;&#x6001;&#xFF0C;&#x800C;&#x6B64;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x4E2D;&#x5E76;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x7EBF;&#x7A0B;&#x8C03;&#x7528; notfiy() &#x548C; notifyAll() &#x65B9;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5; CPU &#x8D44;&#x6E90;&#x518D;&#x6B21;&#x7531; t1 &#x7EBF;&#x7A0B;&#x83B7;&#x5F97;&#x5E76;&#x6267;&#x884C;&#x4EE3;&#x7801;&#xFF0C;&#x8F93;&#x51FA; 5 &#x6761;&#x6253;&#x5370;&#x5185;&#x5BB9;&#xFF0C;t1 &#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#x53D8;&#x4E3A; <code>TERMINATED</code> &#x72B6;&#x6001;&#x3002;&#x6B64;&#x65F6; CPU &#x7684;&#x8D44;&#x6E90;&#x53C8;&#x5207;&#x6362;&#x5230;&#x4E3B;&#x7EBF;&#x7A0B;&#xFF0C;&#x6B64;&#x65F6; isAlive() &#x65B9;&#x6CD5;&#x8FD4;&#x56DE; false&#xFF0C;join() &#x65B9;&#x6CD5;&#x6267;&#x884C;&#x7ED3;&#x675F;&#xFF0C;&#x6700;&#x540E;&#x4E3B;&#x7EBF;&#x7A0B;&#x8F93;&#x51FA; Main over&#x3002;</p>
<p>&#x7531;&#x4EE5;&#x4E0A;&#x5206;&#x6790;&#x53EF;&#x77E5;&#xFF1A;  </p>
<ol>
<li>join() &#x786E;&#x5B9E;&#x662F;&#x7B49;&#x5F85;&#x8BE5;&#x7EBF;&#x7A0B;&#x7EC8;&#x6B62;&#xFF0C;&#x5177;&#x4F53;&#x4E00;&#x70B9;&#x5C31;&#x662F;&#x8C03;&#x7528;&#x8BE5;&#x7EBF;&#x7A0B;(t1)&#x7684;&#x7EBF;&#x7A0B;(&#x4E3B;&#x7EBF;&#x7A0B;)&#xFF0C;&#x5FC5;&#x987B;&#x8981;&#x7B49;&#x5230;&#x8BE5;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x5B8C;&#x6210;&#x4E4B;&#x540E;&#x518D;&#x6267;&#x884C;&#xFF0C;&#x4EE5;&#x4E0A;&#x793A;&#x4F8B;&#x4EC5;&#x4EC5;&#x662F;&#x4EE5;&#x4E3B;&#x7EBF;&#x7A0B;&#x548C;&#x5B50;&#x7EBF;&#x7A0B;&#x4E3A;&#x793A;&#x4F8B;&#x3002;</li>
</ol>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="04-traditional-thread-state.html" class="navigation navigation-prev " aria-label="Previous page: 04-线程状态">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../" class="navigation navigation-next " aria-label="Next page: Linux">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"05-线程join方法","level":"1.11.5","depth":2,"next":{"title":"Linux","level":"1.12","depth":1,"path":"README.md","ref":"README.md","articles":[{"title":"01-Linux常用命令总结","level":"1.12.1","depth":2,"path":"linux/001-linux-command.md","ref":"linux/001-linux-command.md","articles":[]},{"title":"02-Linux防火墙","level":"1.12.2","depth":2,"path":"linux/002-linux-firewall.md","ref":"linux/002-linux-firewall.md","articles":[]},{"title":"03-Linux-Tmux","level":"1.12.3","depth":2,"path":"linux/003-linux-tmux.md","ref":"linux/003-linux-tmux.md","articles":[]}]},"previous":{"title":"04-线程状态","level":"1.11.4","depth":2,"path":"thread/04-traditional-thread-state.md","ref":"thread/04-traditional-thread-state.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["toggle-chapters"],"pluginsConfig":{"toggle-chapters":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"thread/05-traditional-thread-join.md","mtime":"2019-09-08T13:54:11.883Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-12-17T05:25:43.709Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

